const util = {
  get: function (url) {},

  postForm: function (url, data, success, fail) {
    fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      body: this.objToStr(data),
    })
      .then((response) => {
        return response.json();
      })
      .then((resultObj) => {
        success(resultObj);
      })
      .catch((e) => {
        console.error(e);
        if (fail) {
          fail(e);
        }
      });
  },

  objToStr: function (obj) {
    var s = "";
    for (var k in obj) {
      var t = k + "=" + obj[k] + "&";
      s += t;
    }
    return s;
  },
  getToken: function () {
    var token = localStorage.getItem("token");
    return token;
  },
  base64toBlob: function (base64, type = "application/octet-stream") {
    const bstr = atob(base64);
    let n = bstr.length;
    const u8arr = new Uint8Array(n);
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], { type });
  },
  guid: function () {
    return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
      /[xy]/g,
      function (c) {
        var r = (Math.random() * 16) | 0,
          v = c == "x" ? r : (r & 0x3) | 0x8;
        return v.toString(16);
      }
    );
  },
  randomNum: function () {
    const time = Math.round(new Date().getTime() / 1000).toString();
    return time;
  },
  isProd: function () {
    var env = process.env.NODE_ENV;
    if (env === "production") {
      return true;
    }
    return false;
  },
  isDev: function () {
    var env = process.env.NODE_ENV;
    if (env === "development") {
      return true;
    }
    return false;
  },
  postJson: function (url, data, success, fail) {
    console.info(JSON.stringify(data))
    fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(data),
    })
      .then((response) => {
        return response.json();
      })
      .then((resultObj) => {
        success(resultObj);
      })
      .catch((e) => {
        console.error(e);
        if (fail) {
          fail(e);
        }
      });
  }
};
export default util;
